/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dtuanh
 */
import java.io.DataInputStream;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {
    //ajouter une nouvelle entity
    public static void addEntity(DataInputStream in, Dictionnary dict) throws Exception
            //(String term, CategoryType categ, Property[] props, String def)
    {
    //prendre le term
    String term=readTerm(in);
    //prendre le category type
    CategoryType categ=readCategoryType(in);
    PropertyImpl[] props=new PropertyImpl[2];
    switch(categ)
    {
        case NOM:
            props[0]=new PropertyImpl();
            props[0].setPropertyType(PropertyType.GENRE);
            props[0].setGenre(readGenre(in));
            props[1]=new PropertyImpl();
            props[1].setPropertyType(PropertyType.PLURIEL);
            props[1].setPluriel(readPluriel(in));
            break;
        case ADJ:
            props[0]=new PropertyImpl();
            props[0].setPropertyType(PropertyType.FEMININ);
            props[0].setFeminin(readFeminin(in));
            props[1]=new PropertyImpl();
            props[1].setPropertyType(PropertyType.PLURIEL);
            props[1].setPluriel(readPluriel(in));
            break;
        case  VERB:
            props[0]=new PropertyImpl();
            props[0].setPropertyType(PropertyType.GROUPE);
            props[0].setGroupe(readGroup(in));
            props[1]=new PropertyImpl();
            props[1].setPropertyType(PropertyType.TRANSIVITE);
            props[1].setTrans(readTrans(in));
            break;
    }
    //prendre la definition du terme
    String def =readDefinition(in)  ;

    dict.addEntity(term, categ, props, def);
    }
    //afficher l'information d'une entity
  public static void showEntity(Entity entity) throws Exception
            //(String term, CategoryType categ, Property[] props, String def)
    {
    String term=entity.myEntry().getTerm();
       System.out.println("-----------------Debut---------------");
       System.out.println("Term: "+term);
    CategoryType categ=entity.myEntry().myCategory().getCategory();
    Property[] props=entity.myEntry().myProperties();
    switch(categ)
    {
        case NOM:
           System.out.println("Category: Nom");
           System.out.println("Genre: "+props[0].printString());
           System.out.println("Pluriel: "+props[1].printString());
            break;
        case ADJ:
          System.out.println("Category: Adjective");
          System.out.println("Feminin: "+props[0].printString());
          System.out.println("Pluriel: "+props[1].printString());
        case  VERB:
          System.out.println("Category: Verbe");
          System.out.println("Group: "+props[0].printString());
          System.out.println("Transitivite: "+props[1].printString());
    }
    System.out.println("Definition:"+entity.myDefinition());
      System.out.println("---------------Fin---------------");

    }
//prendre le term
    private static String readTerm(DataInputStream in) throws IOException {
        System.out.print("Entrez term:");
        return in.readLine();
    }
//prendre la type de category
    private static CategoryType readCategoryType(DataInputStream in) throws IOException {
        while (true) {

            System.out.print("Entrez Category [1=Nom], [2=Adj], [3=Verb]:");
            String s = in.readLine();
            if (s.equals("1")) {
                return CategoryType.NOM;
            }
            if (s.equals("2")) {
                return CategoryType.ADJ;
            }
            if (s.equals("3")) {
                return CategoryType.VERB;
            }

        }
    }

 
//prendre la definition du terme
    private static String readDefinition(DataInputStream in) throws IOException {
        System.out.print("Entrez definition:");
        return in.readLine();
    }

    //Prendre la genre
    private static CodeGenre readGenre(DataInputStream in) throws IOException {
        //MASC, FEM
        while (true) {
            System.out.print("Entrez Genre [1=masculin],[ 2=Feminin]:");
            String s = in.readLine();
            if (s.equals("1")) {
                return CodeGenre.MASC;
            }
            if (s.equals("2")) {
                return CodeGenre.FEM;
            }
        }
    }

    //Prendre le pluriel du terme
     private static String readPluriel(DataInputStream in) throws IOException {
            System.out.print("Entrez pluriel:");
           return in.readLine();
        }

     //prendre le feminin du terme
    private static String readFeminin(DataInputStream in) throws IOException {
            System.out.print("Entrez feminin:");
           return in.readLine();
        }

    //prendre la transitivite
     private static CodeTrans readTrans(DataInputStream in) throws IOException {
        //MASC, FEM
        while (true) {
     
            System.out.print("Entrez transitivite [1=Transitif], [2=Intransitif]:");
            String s = in.readLine();
            if (s.equals("1")) {
                return CodeTrans.TRANS;
            }
            if (s.equals("2")) {
                return CodeTrans.INTR;
            }
        }
    }

     //prendre le numuro du groupe
 private static short readGroup(DataInputStream in) throws IOException {
               while (true) {
            
            System.out.print("Entrez groupe number[1,2,3]:");
            String s = in.readLine();
            if (s.equals("1")) {
                return 1;
            }
            if (s.equals("2")) {
                return 2;
            }
            if (s.equals("3")) {
                return 3;
            }
            else
            {
                System.out.println(" nombre n'est pas valid");
                in.readChar();
                continue;
            }

        }
 }
  //prendre la direction de trier
 // ascendant (terme) si direct = TRUE, sinon descendant
 private static boolean readDirection(DataInputStream in) throws IOException {
               while (true) {

            System.out.print("Entrez direction[0=ascendant,1=descendant]:");
            String s = in.readLine();
            if (s.equals("0")) {
                return true;
            }
            if (s.equals("1")) {
                return false;
            }
            else
            {
                System.out.println(" nombre n'est pas valid");
                in.readChar();
                continue;
            }

        }
 }

    public static void main(String args[]) throws Exception {
        DataInputStream in = new DataInputStream(System.in);
          Dictionnary dict=null;

         // if (System.getSecurityManager() == null) {
       // System.setSecurityManager(new SecurityManager());
        //}
        try {
        String name = "Dict";
        Registry registry = LocateRegistry.getRegistry(args[0]);
        dict = (Dictionnary) registry.lookup(name);
      
        } catch (Exception e) {
        System.err.println(" exception:");
        e.printStackTrace();
        }
            System.out.println("[0]Aide");
            System.out.println("[1]Ajouter nouvel mot");
            System.out.println("[2]Supprimer un mot");
            System.out.println("[3]Changer un mot");
            System.out.println("[4]Charger dictionnaire");
            System.out.println("[5]Sauvegarder dictionnaire");
            System.out.println("[6]Consulter un mot");
            System.out.println("[7]Premier mot");
            System.out.println("[8]Dernier mot");
            System.out.println("[9]Mot suivant");
            System.out.println("[10]Mot precedent");
            System.out.println("[11]Total mots");
            System.out.println("[12]Trier");
            System.out.println("[13]Quitter");
        while (true) {
    
          
            System.out.print("Commande:");
                    String cmd = in.readLine();
            try{
                //afficher aide
            if (cmd.equalsIgnoreCase("0")) {
            System.out.println("[0]Aide");
            System.out.println("[1]Ajouter nouvel mot");
            System.out.println("[2]Supprimer un mot");
            System.out.println("[3]Changer un mot");
            System.out.println("[4]Charger dictionnaire");
            System.out.println("[5]Sauvegarder dictionnaire");
            System.out.println("[6]Consulter un mot");
            System.out.println("[7]Premier mot");
            System.out.println("[8]Dernier mot");
            System.out.println("[9]Mot suivant");
            System.out.println("[10]Mot precedent");
            System.out.println("[11]Total mots");
            System.out.println("[12]Trier");
            System.out.println("[13]Quitter");

            }
            //ajouer un mot
            else if (cmd.equalsIgnoreCase("1")) {
                Client.addEntity(in, dict);


            }
            //supprimer un mot
            else if (cmd.equalsIgnoreCase("2")) {
               dict.delEntity(readTerm(in));
               System.out.println("BIen Supprimer!");
            }
            //changer un mot
            else if (cmd.equalsIgnoreCase("3")) {
                System.out.print("Entrez mot pour modifier:");
                String old=in.readLine();
                System.out.print("Entrez nouvelle definition de "+old+":");
                String def=in.readLine();
                dict.modifDefinition(old, def);
            }
            //charger la dictionnaire
             else if (cmd.equalsIgnoreCase("4")) {
                long n=dict.loadDict();
                System.out.println("Total "+n+"  entries!");
            }
            //sauvegarder la dictionnaire
             else if (cmd.equalsIgnoreCase("5")) {
                dict.saveDict();
                 System.out.println("Bien sauvegarder!");
            }
            //conseiller un mot
             else if (cmd.equalsIgnoreCase("6")) {
                 System.out.print("Entrez un mot:");
                 String word=in.readLine();
                 Entity entity=dict.findEntity(word);
                 showEntity(entity);

            }
            //afficher premier mot
             else if (cmd.equalsIgnoreCase("7")) {
                 Entity entity=dict.fistEntity();
                 showEntity(entity);

            }
            //afficher dernier mot
             else if (cmd.equalsIgnoreCase("8")) {
                 Entity entity=dict.lastEntity();
                 showEntity(entity);

            }
            //afficher le mot suivant
             else if (cmd.equalsIgnoreCase("9")) {
                 Entity entity=dict.nextEntity();
                 showEntity(entity);

            }
            //afficher le mot precedent
           else if (cmd.equalsIgnoreCase("10")) {
                 Entity entity=dict.prevEntity();
                 showEntity(entity);

            }
            //afficher total nombre de mots
            else if (cmd.equalsIgnoreCase("11")) {
                long n=dict.numEntities();
                 System.out.println("Total "+n+" entries ");

            }
            //Trier la dictionnaire
             else if (cmd.equalsIgnoreCase("12")) {
                 dict.sortDict(readDirection(in));
                 System.out.println("Trier...  ");
            }
            //quiiter
             else if (cmd.equalsIgnoreCase("13")) {
                 System.exit(0);

            }
             else
             {
                System.out.println("Invalid command, type 0 to help");
             }
            }catch(Exception rx)
            {
              System.out.println("Error:"+rx);
              rx.printStackTrace();
            }

        }
        /*
       */
    }
}
